using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core.Objects;
using Web.Core;

public partial class AdminCP_Orders : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Language"] == null)
            Session["Language"] = Web.Core.GlobalStorage.Language.English;

        int page = 1;
        if (Request.Params["page"] != null)
            page = int.Parse(Request.Params["page"]);
        ArrayList arrOrders = Orders.GetOrderByPage(page, 10, 0);
        
        if (!IsPostBack)
        {
            this.rptOrders.DataSource = arrOrders;
            this.rptOrders.DataBind();
        }
        // Pager
        this.Pager1.objectsPerPage = 10;
        this.Pager1.displayAll = false; // Chi hien thi ben phai
        this.Pager1.totalObjects = Orders.CountAllOrders();
        // ---------------

        if (Request.Params["do"] != null)
        {
            string pDo = Request.Params["do"];
            switch (pDo.ToLower())
            {
                case "del":
                    if (Request.Params["oid"] != null)
                    {
                        int orderID = 0;
                        try
                        {
                            orderID = int.Parse(Request.Params["oid"].ToString());
                        }
                        catch { }
                        Orders.DeleteOrder(new Order(orderID, 0, GlobalStorage.State.Any, ""));
                        this.PreRenderComplete += new EventHandler(AdminCP_Orders_PreRenderComplete);
                    }
                    break;
                case "tocompleted":
                    if (Request.Params["oid"] != null)
                    {
                        int orderID = 0;
                        try
                        {
                            orderID = int.Parse(Request.Params["oid"].ToString());
                        }
                        catch { }
                        Order o = Orders.GetOrderByID(orderID);
                        o.State = GlobalStorage.State.Complete;
                        Orders.UpdateOrder(o);
                        this.PreRenderComplete += new EventHandler(AdminCP_Orders_PreRenderComplete);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    void AdminCP_Orders_PreRenderComplete(object sender, EventArgs e)
    {
        Response.Redirect("Orders.aspx");
    }

    public string GetState(int orderID)
    {
        string result = "";
        Order o = Orders.GetOrderByID(orderID);
        switch (o.State)
        {
            case GlobalStorage.State.Order:
                result = Resources.Lang.StateOrder;
                break;
            case GlobalStorage.State.Shipping:
                result = "<a id=\"" + orderID + "\" href=\"#\" onclick=\"ConfirmToComplete(this.id);\"><b>" + Resources.Lang.StateWaitingGoods + "</b></a>";
                break;
            case GlobalStorage.State.Complete:
                result = Resources.Lang.StateCompleted;
                break;
        }
        return result;
    }
}
